<?php
namespace app\modules\models;
use yii\db\ActiveRecord;
use Yii;
class Admin extends ActiveRecord{
	public $rememberMe = true;
	public $repass;
	// public static function tableName(){
	// 	return '{{%admin}}';
	// }
	public function attributeLabels(){
		return [
			'adminuser'=>'管理员账号',
			'adminemail'=>'管理员邮箱',
			'adminpass'=>'管理员密码',
			'repass'=>'确认密码',
		];
	}
	public function rules(){
		return [
			['adminuser','required','message'=>'管理员账号不能为空','on'=>['login','seekPass','changePass','addManager','changeEmail']],
			['adminuser','unique','message'=>'管理员账号已存在','on'=>'addManager'],
			['adminpass','required','message'=>'管理员密码不能为空','on'=>['login','changePass','addManager','changeEmail']],
			['rememberMe','boolean','on'=>'login'],
			['adminpass','validatePass','on'=>['login','changeEmail']],//匹配管理员账号和密码
			['adminemail','required','message'=>'电子邮箱不能为空','on'=>['seekPass','addManager','changeEmail']],
			['adminemail','email','message'=>'电子邮箱格式不正确','on'=>['seekPass','addManager','changeEmail']],
			['adminemail','unique','message'=>'电子邮箱已被注册','on'=>['addManager','changeEmail']],
			['adminemail','validateEmail','on'=>'seekPass'],//匹配管理员账号和邮箱
			['repass','required','message'=>'确认密码不能为空','on'=>['changePass','addManager']],
			['repass','compare','compareAttribute'=>'adminpass','message'=>'两次密码输入不一致','on' => ['changePass','addManager']],
		];
	}

/**
 * 验证密码
 * @author 	zhuojundu 
 */
	public function validatePass(){
		if(!$this->hasErrors()){
			$data = self::find()->where('adminuser = :user and adminpass = :pass',[':user'=>$this->adminuser,":pass"=>md5($this->adminpass)])->one();
			if(is_null($data)){
				$this->addError('adminpass',"管理员账号或密码错误");
			}
		}
	}
/**
 * 登陆操作
 * @author 	zhuojundu
 */
	public function login($data){
		$this->scenario = 'login';
		if($this->load($data) && $this->validate()){
			//记住用户
			$lifetime = $this->rememberMe ? 24*3600 : 0;
			$session = Yii::$app->session;
			session_set_cookie_params($lifetime);
			$session['admin'] = [
				'adminuser' => $this->adminuser,//用户名
				'isLogin' => 1,//是否登陆，1：是，0:否
			];
			$this->updateAll(['logintime'=>time(),'loginip'=>ip2long(Yii::$app->request->userIP)],'adminuser = :user',[':user'=>$this->adminuser]);
			return (bool)$session['admin']['isLogin'];
		}
		return false;	
	}
/**
 *	验证邮箱 
 * @author 	zhuojundu
 */
	public function validateEmail(){
		if(!$this->hasErrors()){
			$data = self::find()->where('adminuser = :user and adminemail = :email',[':user'=>$this->adminuser,':email'=>$this->adminemail])->one();
			if(is_null($data)){
				$this->addError("adminemail","管理员邮箱账号不匹配");
			}
		}
	}
/**
 * 找回密码
 * @author   zhuojundu
 */
	public function seekPass($data){
		$this->scenario = 'seekPass';
		if($this->load($data) && $this->validate()){
			$time = time();
			$token = $this->createToken($data['Admin']['adminuser'],$time);
			//邮箱操作
			$mailer = Yii::$app->mailer->compose('seekpass',['adminuser'=>$data['Admin']['adminuser'],'time'=>$time,'token'=>$token]);
			$mailer->setFrom('13824455761@163.com');//发件人
			$mailer->setTo($data['Admin']['adminemail']);//收件人
			$mailer->setSubject('邮箱测试');
			if($mailer->send()){
				//发送成功
				return true;
			}
		}
		return false;
	}

/**
 * 获取token
 * @author   zhuojundu
 */
	public function createToken($adminuser, $time){
		return md5(md5($adminuser).base64_encode(Yii::$app->request->userIP).md5($time));
	}

/**
 * 忘记密码->修改密码
 * @author    zhuojundu
 */
	public function changePass($data){
		$this->scenario = 'changePass';
		if($this->load($data) && $this->validate()){
			return (bool)$this->updateAll(['adminpass'=>md5($this->adminpass)],'adminuser = :user',[':user'=>$this->adminuser]);
		}
		return false;
	}

/**
 * 新增管理员
 * @author   zhuojundu
 */
	public function addManager($data){
		$this->scenario = 'addManager';
		// print_r($this->getErrors());die;
		if($this->load($data) && $this->validate()){
			$this->adminpass = md5($data['Admin']['adminpass']);
			if($this->save(false)){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}

/**
 * 修改个人信息
 */
	public function changeEmail($data){
		$this->scenario = 'changeEmail';
		if($this->load($data) && $this->validate()){
			return (bool)$this->updateAll(['adminemail'=>$data['Admin']['adminemail']],'adminuser =:user',[':user'=>$this->adminuser]);
		}
		return false;
	}








}